package com.github.winyiwin.queue;

import com.github.winyiwin.KobeRejectedPolicy;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: secoo
 * @date: 2020/6/19 15:30
 */
public enum RejectedHandlerEnum {
    AbortPolicy {
        @Override
        public RejectedExecutionHandler createRejectedHandler() {
            return new ThreadPoolExecutor.AbortPolicy();
        }
    },
    DiscardPolicy {
        @Override
        public RejectedExecutionHandler createRejectedHandler() {
            return new ThreadPoolExecutor.DiscardPolicy();
        }
    },
    DiscardOldestPolicy {
        @Override
        public RejectedExecutionHandler createRejectedHandler() {
            return new ThreadPoolExecutor.DiscardOldestPolicy();
        }
    },
    CallerRunsPolicy {
        @Override
        public RejectedExecutionHandler createRejectedHandler() {
            return new ThreadPoolExecutor.CallerRunsPolicy();
        }
    },
    KobeRejectedPolicy {
        @Override
        public RejectedExecutionHandler createRejectedHandler() {
            return null;
        }
    };

    public abstract RejectedExecutionHandler createRejectedHandler();


    public static RejectedHandlerEnum getInstance(RejectedHandlerEnum handlerEnum) {
        for (RejectedHandlerEnum value : RejectedHandlerEnum.values()) {
            if (value == KobeRejectedPolicy) {
                break;
            }
            if (value == handlerEnum) {
                return value;
            }
        }
        return null;
    }
}
